Home:ALL Converter>C to MIPS translation: bubble sort

C to MIPS translation: bubble sort

Ask Time:2019-03-03T06:56:54         Author:user611988

Json Formatter

I'm given the code in C and asked to translate it into MIPS.

##struct Ingredient {
##  unsigned ing_type;
##  unsigned amount;
##};
##
##struct Request {
##  unsigned length;
##  Ingredient ingredients [11];
##};
##
##//Performs a bubble sort on the given request using the given comparison function
##void bubble_sort(Request* request, int (*cmp_func) (Ingredient*, Ingredient*)) {
##    for (int i = 0; i < request->length; ++i) {
##        for (int j = 0; j < request->length - i - 1; ++j) {
##            if (cmp_func(&request->ingredients[j], &request->ingredients[j + 1]) > 0) {
##                Ingredient temp = request->ingredients[j];
##                request->ingredients[j] = request->ingredients[j + 1];
##                request->ingredients[j + 1] = temp;
##            }
##        }
##    }
##}

This is what I have so far but I am struggling with using the int(*cmp_func). I know that it is a pointer to a comparator function but how do loop through?

    li $t0, 0            # set i = 0
    li $t1, 0            # set j = 0
    add $t2, $a0, 0  # request->length

for1:
    beq $t0, $t2, end    # if i >= request->length, go to between_loops
    li $t1, 0                    # reset j to 0 after each iteration
    addi $t0, $t0, 1         # ++i
    j for2

for2:
    sub $t3, $t2, $t0           # t3 = request->length - i
    sub $t3, $t3, 1             # t3 = request->length - i - 1
    beq $t1, $t3, for1    # if j >= request->length - i - 1, go to for1

    # if statement goes here

    addi $t1, $t1, 1         # ++j
    j for2

Author:user611988,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54963862/c-to-mips-translation-bubble-sort
yy